package cn.iocoder.yudao.module.yyzx.controller.admin.check_In;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotations.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.yyzx.controller.admin.check_In.vo.*;
import cn.iocoder.yudao.module.yyzx.dal.dataobject.check_In.Check_InDO;
import cn.iocoder.yudao.module.yyzx.service.check_In.Check_InService;

@Tag(name = "管理后台 - 入住登记")
@RestController
@RequestMapping("/yyzx/check_-in")
@Validated
public class Check_InController {

    @Resource
    private Check_InService check_InService;

    @PostMapping("/create")
    @Operation(summary = "创建入住登记")
    @PreAuthorize("@ss.hasPermission('yyzx:check_-in:create')")
    public CommonResult<Integer> createCheck_In(@Valid @RequestBody Check_InSaveReqVO createReqVO) {
        return success(check_InService.createCheck_In(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新入住登记")
    @PreAuthorize("@ss.hasPermission('yyzx:check_-in:update')")
    public CommonResult<Boolean> updateCheck_In(@Valid @RequestBody Check_InSaveReqVO updateReqVO) {
        check_InService.updateCheck_In(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除入住登记")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('yyzx:check_-in:delete')")
    public CommonResult<Boolean> deleteCheck_In(@RequestParam("id") Integer id) {
        check_InService.deleteCheck_In(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得入住登记")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('yyzx:check_-in:query')")
    public CommonResult<Check_InRespVO> getCheck_In(@RequestParam("id") Integer id) {
        Check_InDO check_In = check_InService.getCheck_In(id);
        return success(BeanUtils.toBean(check_In, Check_InRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得入住登记分页")
    @PreAuthorize("@ss.hasPermission('yyzx:check_-in:query')")
    public CommonResult<PageResult<Check_InRespVO>> getCheck_InPage(@Valid Check_InPageReqVO pageReqVO) {
        PageResult<Check_InDO> pageResult = check_InService.getCheck_InPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, Check_InRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出入住登记 Excel")
    @PreAuthorize("@ss.hasPermission('yyzx:check_-in:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCheck_InExcel(@Valid Check_InPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<Check_InDO> list = check_InService.getCheck_InPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "入住登记.xls", "数据", Check_InRespVO.class,
                        BeanUtils.toBean(list, Check_InRespVO.class));
    }

}